package com.cskaoyan.controller.admincontroller;

import com.cskaoyan.util.ValidationUtil;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.chaosbean.system.vo.SystemListVo;
import com.cskaoyan.exception.ValidationException;
import com.cskaoyan.service.adminservice.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jackX
 * @datetime 2022/12/28 14:45
 * @description: TODO:
 */
@RestController
@RequestMapping("admin/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    /**
     * 管理员页面的分页显示
     * @param basePageInfo 接收分页数据
     * @param username 根据管理员名称查询的所需条件
     * @return
     */
    //http://localhost:8083/admin/admin/list?page=1&limit=20&sort=add_time&order=desc

    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo, String username) {
        SystemListVo listVo = adminService.listAdmin(basePageInfo, username);

        return BaseRespVo.okYa(listVo);
    }

    /**
     * 增加管理员
     * @param marketAdmin 接收页面传递的管理员信息
     * @return
     */
    //http://182.92.235.201:8083/admin/admin/create
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody @Validated MarketAdmin marketAdmin, BindingResult bindResult) throws ValidationException {
        ValidationUtil.valid(bindResult);
        //增加管理员
        BaseRespVo createAdmin = adminService.create(marketAdmin);
        return createAdmin;
    }

    /**
     * 更新管理员
     * @param marketAdmin 接收页面传递的管理员信息
     * @return
     */
    //http://182.92.235.201:8083/admin/admin/update
    @RequestMapping("update")
    public BaseRespVo updateAdmin(@RequestBody MarketAdmin marketAdmin) {
        //更新管理员
        BaseRespVo update = adminService.update(marketAdmin);
        return update;
    }

    /**
     * 删除管理员
     * @param marketAdmin 接收页面传递的管理员信息
     * @return
     */
    //http://182.92.235.201:8083/admin/admin/delete
    @RequestMapping("delete")
    public BaseRespVo deleteAdmin(@RequestBody MarketAdmin marketAdmin) {
        Integer id = marketAdmin.getId();
        //删除管理员
        adminService.delete(id);
        return BaseRespVo.ok();
    }


}
